﻿Public Class ImageViewer
    Public Sub png()
        PictureZoom1.BackgroundImage = My.Resources.bg
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim style As New ToolStripProfessionalRenderer(New MicrosoftOffice)
        ToolStrip1.Renderer = style
        Me.ActiveControl = PictureZoom1
    End Sub
    Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
        If MsgBox("You want to delete this file... Are you sure?", 36, "Delete image") = MsgBoxResult.Yes Then
            Try
                System.IO.File.Delete(Me.Text)
                Me.Close()
                GeneralForm.refreshfiles()
            Catch ex As Exception
                MsgBox("An error occured: " + ex.Message)
            End Try
        Else

        End If
    End Sub

    Private Sub RenameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RenameToolStripMenuItem.Click
        RenameDialog.imagerename = True
        RenameDialog.lastname = Me.Text
        RenameDialog.ext = System.IO.Path.GetExtension(Me.Text)
        RenameDialog.ShowDialog()
    End Sub

    Private Sub ImageViewer_ResizeEnd(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.ResizeEnd
        Me.Invalidate()
        PictureZoom1.Refresh()
    End Sub
End Class

Public Class PictureZoom
    Inherits PictureBox
    Private zmLevel As Integer = 1
    Private zmPt As Point
    Overloads Property Image() As Image ' we want to hook when client's set the image to reset the zoom level to unzoomed
        Get
            Return MyBase.Image
        End Get
        Set(ByVal value As Image)
            zmLevel = 1 ' reinit
            MyBase.Image = value
        End Set
    End Property
    Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
        'MyBase.OnPaint(pe) ' don't call baseclass to paint
        If Me.Image IsNot Nothing Then
            Dim loc As Point
            Dim sz As Size
            If zmLevel <> 1 Then
                sz = New Size(Me.Image.Width / zmLevel, Me.Image.Height / zmLevel)
                ' center on zmPt
                loc = New Point(Me.Image.Width * (zmPt.X / Me.ClientRectangle.Width) - sz.Width / 2, _
                                Me.Image.Height * (zmPt.Y / Me.ClientRectangle.Height) - sz.Height / 2)  '
            Else
                loc = New Point(0, 0)   ' no zoom: we want the entire source picture
                sz = Me.Image.Size
            End If
            Dim rectSrc = New Rectangle(loc, sz)
            ' now draw the rect of the source picture in the entire client rect of MyPictureBox
            pe.Graphics.DrawImage(Me.Image, Me.ClientRectangle, rectSrc, GraphicsUnit.Pixel)
        End If
    End Sub
    Sub PictureBox_MouseWheel(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseWheel
        If Me.zmLevel = 1 Then  ' can only anchor when unzoomed
            Me.zmPt = New Point(e.X, e.Y)
        End If
        If e.Delta > 0 Then
            If zmLevel < 20 Then
                zmLevel += 1
            End If
        Else
            If e.Delta < 1 Then
                If zmLevel > 1 Then
                    zmLevel -= 1
                End If
            End If
        End If
        Me.Invalidate() ' queue msg to repaint
    End Sub

End Class